package com.zhumo.sz.common.utils;


/**
 * @author jiqi.zhu
 * @comment 地理工具类
 */
public class GeographyUtil {

    public static final double RADIUS_OF_EARTH = 6371000; // 地球半径（单位：米）
    // 将角度转换为弧度
    private static double toRadians(double degree) {
        return degree * Math.PI / 180.0;
    }



    /**
     * 计算经纬度坐标距离
     */
    public static double distanceByCoordinate(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = toRadians(lat1);
        double lon1Rad = toRadians(lon1);
        double lat2Rad = toRadians(lat2);
        double lon2Rad = toRadians(lon2);

        double deltaLat = lat2Rad - lat1Rad;
        double deltaLon = lon2Rad - lon1Rad;

        double a = Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.pow(Math.sin(deltaLon / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return RADIUS_OF_EARTH * c;
    }

//    public static void main(String[] args) {
//        double lat1 = 52.2296756;
//        double lon1 = 21.0122287;
//        double lat2 = 41.8781136;
//        double lon2 = -87.6297982;
//
//        double distance = distance(lat1, lon1, lat2, lon2);
//        System.out.println("距离：" + distance + " 米");
//    }
}
